import java.util.*;

class Student {
    private Integer no;
    private String name;
    public Student(Integer no, String name) {
        this.no = no;
        this.name = name;
    }
    public Integer getNo() {
        return no;
    }
    public void setNo(Integer no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return String.format("\n学号：%s，姓名:%s\n", this.no, this.name);
    }
}

public class Test {
    public static void listExample() {
        // 默认每个存储单元存储Object类型
        // 列表中只能存储引用类型，即对象，不能是基础数据类型
        List objList = new ArrayList();
        objList.add(new Integer(1)); //存于0号单元
        objList.add(new String("软件工程"));//存于1号单元
        objList.add(new Float(1.2f));//存于2号单元
        objList.add(new Double(2.5));//存于3号单元
//        objList.add(0,new String("面向对象"));
        for (int i = 0; i < objList.size(); i++) {
            System.out.println(objList.get(i));
        }
        for (Object o : objList) {
            System.out.println(o);
        }
        Object o = objList.get(2);
        //规定list只能存储String类型数据,将泛型变元T转变成String类型
        List<String> list = new ArrayList<>();
        list.add("How ");
        list.add("are ");
        list.add("you?");
        // list.add(5);//不能存储整型数据
        for (String s : list) {
            System.out.print(s);
        }
        List<Integer> anotherList = new LinkedList<>();
        anotherList.add(new Integer(1));
    }

    public static void setExample() {
        Set<Integer> sets = new HashSet<>();
        sets.add(3);
        sets.add(1);
        sets.add(2);
        sets.add(1);
        System.out.println(sets.size());
        Set<Integer> sortSets = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        sortSets.addAll(sets);
        System.out.println(sets.toString());
        Iterator<Integer> iterator = sortSets.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            System.out.print(next + " ");
        }
    }

    public static void mapExample() {
        // String为key的类型，Integer为value的类型
        Map<String, Integer> map = new HashMap<>();
        map.put("one", new Integer(1));
        map.put("two", new Integer(2));
        map.put("three", new Integer(3));
        map.put("four", new Integer(4));
        for (String key : map.keySet()) {
            Integer integer = map.get(key);
            System.out.print(integer + " ");
        }
        // Integer为key的类型，Integer为value的类型
        Map<String, Student> studentMap = new HashMap<>();
        Student stu1 = new Student(1001, "刘德华");
        Student stu2 = new Student(1002, "郭富城");
        studentMap.put("stu1001", stu1);
        studentMap.put("stu1002", stu2);
        Set<String> keySet = studentMap.keySet();
        for (String key : keySet) {
            Student student = studentMap.get(key);
            System.out.print(student);
        }
    }

    public static void main(String[] args) {
        listExample();
        setExample();
        mapExample();
    }
}
